//
//  JoinCompanyVC.m
//  ZuLinProject
//
//  Created by ypj on 2021/12/8.
//

#import "JoinCompanyVC.h"
#import "PersonCenterViewController.h"

@interface JoinCompanyVC ()<UITextFieldDelegate>

@property (nonatomic, strong) UITextField *invitTF;

@end

@implementation JoinCompanyVC

- (void)viewWillAppear:(BOOL)animated
{
    [self leftButton];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self layoutCustomNav:self.view title:@"加入企业"];
    
    UILabel *alertLB = [[UILabel alloc] initWithFrame:CGRectMake(20, kViewTopHeight + 24, ScreenWidth - 20, 17)];
    alertLB.text = @"输入您即将加入企业的邀请码";
    alertLB.font = [UIFont systemFontOfSize:12];
    alertLB.textColor = ColorWith3C6CFC;
    [self.view addSubview:alertLB];
    
    UIView *invitationView = [[UIView alloc] initWithFrame:CGRectMake(20, alertLB.Y_height + 14, ScreenWidth - 40, 42)];
    invitationView.backgroundColor = ColorWithF5F5F5;
    [self.view addSubview:invitationView];
    
    UILabel *invitationLB = [[UILabel alloc] initWithFrame:CGRectMake(12, 0, 60, invitationView.height)];
    invitationLB.text = @"邀请码:";
    invitationLB.font = [UIFont systemFontOfSize:16];
    invitationLB.textColor = Black_Color;
    [invitationView addSubview:invitationLB];
    
    self.invitTF = [[UITextField alloc] initWithFrame:CGRectMake(invitationLB.X_width + 1, 0, 200, invitationView.height)];
    _invitTF.delegate = self;
    [invitationView addSubview:_invitTF];
    
    UIButton *joinBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    joinBtn.frame = CGRectMake(20, ScreenHeight - 42 - 48, ScreenWidth - 40, 48);
    [joinBtn setTitle:@"立即加入" forState:(UIControlStateNormal)];
    [joinBtn setTitleColor:White_Color forState:(UIControlStateNormal)];
    [joinBtn setBackgroundColor:ColorWith3C6CFC];
    joinBtn.layer.masksToBounds = YES;
    joinBtn.layer.cornerRadius = 4;
    [self.view addSubview:joinBtn];
    [joinBtn addTarget:self action:@selector(joinAction) forControlEvents:(UIControlEventTouchUpInside)];
}

-(void)joinAction
{
    if (self.invitTF.text.length == 0 || [self.invitTF.text isEqualToString:@" "]){
        showAlert(@"邀请码不能为空");
        return;
    }
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    [params setObject:self.invitTF.text  forKey:@"inviteCode"];
  
    
    [MSNetworkManager postRevertUrl:POSTAddCompany_URL params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
            YLUserInfo * info = [YLUserMgr fetchUser];
            NSDictionary *sysUser = resonseObj[@"userInfo"][@"sysUser"];
            [self saveUserInfo:sysUser];
            
            info.nickName = sysUser[@"nickName"];
            info.enterpriseName = sysUser[@"enterpriseName"];
            info.workAddress = sysUser[@"workAddress"];
            info.username = sysUser[@"username"];
            info.carNum = sysUser[@"carNum"];
            info.logoUrl = sysUser[@"logoUrl"];
            info.avatar =sysUser[@"avatar"];
            
            NSDictionary *inviteCode = resonseObj[@"inviteCode"];
            info.roleId = inviteCode[@"roleId"];
            info.roleName = inviteCode[@"roleName"];
            
            
            NSDictionary *user = resonseObj[@"user"];
            info.companyId = user[@"companyId"];
            
            [[MSStorageUtils shareInstance] userDefaultSave:info.roleName withKey:COMPANY_ROLE_NAME];
            
            // 获取个人信息保存
            [self getUserInfo:^(BOOL isSuccess) {
            }];
           
            [JPUSHService setAlias:USER_ID completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
                NSLog(@"");
            } seq:1];
            
            
            
            [YLUserMgr saveUser:info];
            if (self.isFromeLogin == YES){
                UIWindow *currentWindow=[BaseViewController getCurrentWindow];
                LiXingMainTabbarVC *tabBarController = [[LiXingMainTabbarVC alloc] init];
                tabBarController.delegate = self;
                tabBarController.selectedIndex = 0;
                [currentWindow setRootViewController:tabBarController];
                [currentWindow makeKeyAndVisible];
            } else {
                for (UIViewController *controller in self.navigationController.viewControllers){
                    if ([controller isKindOfClass:[PersonCenterViewController class]]){
                        PersonCenterViewController *messageVC =(PersonCenterViewController *)controller;
                        [self.navigationController popToViewController:messageVC animated:YES];
                    }
                }
            }
        }
        
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
  
}


@end
